<?


/** dzkone readset functionality.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author: JQ + Henri Ranki 11.6.2001
 *  $Id: dzkone_delete.php,v 1.3 2005/03/12 17:06:54 jyry Exp $
 */
 
include_once("sodebug.php");

/** Deletes a set
 *  @param dzKone $Kone dzKone object to operate with.
 *  @return bool Returns true(1) when deleted successfully or false(0) in case of
 *  an error such as locking failed.
 */
function __dzkone__deleteSet(&$Kone) {
    include("sodebug.inc");
    include_once("dzkone_lock.php");
    
    soDebug("dzKone->__deleteSet: enter", SOD_DET);

    if (!__dzkone__lockSet($Kone, SOLM_OWN)) return 0; // <-- EXIT

    /* Error flag, see end of proc for more details  */
    $error = 0;

    /* Delete count: important for the end result/error status  */
    $deleted = 0;

    /* Start transaction */
    $Kone->__Soda->runTransact("begin");

    $Kone->__Soda->setAction(SODA_DELETE);

    /* loop tables and delete as much as possible :-) */
    $tables = $Kone->__getColsByTable();
    $tablenames = $tables->getkeys();
    $sql_result = 1;
    $tcount = count($tablenames);
    $t = -1;
    while ((!$error) && (++$t < $tcount)) {
        $tablename = $tablenames[$t];

        $Kone->__Soda->clearAllSections();
        $soda = $tables->getItem($tablename);
        $Kone->__Soda->setTable($tablename, 1);

        /* Add where columns */
        $cols = implode(" ", $soda["wherecols"]);
        $vals = $soda["wherevals"];
        $Kone->__Soda->setSection(SODA_WHERE, $cols, $vals, $method);

        $sql_result = $Kone->__Soda->run();

        if ($sql_result) {
            $deleted++;
        } else {
            $error = 1;
            $Kone->__OutItu->setBlockItem(DZI_CONTROL, DZK_RESULT,
                                          DZR_DELETE_ERROR);
            $Kone->__OutItu->setBlockItem(DZI_DATA, DZK_SQL,
                                          $Kone->__Soda->Clause);
        }; // if !$sql_result
    }; // while

    /* Non deleted, error */
    if ($deleted == 0) $error = 1;

    /* Start transaction */
    if ($sql_result) {
        $end_transact = "commit";
    } else {
        $end_transact = "rollback";
    }; // if
    $Kone->__Soda->runTransact($end_transact);

    if (__dzkone__lockSet($Kone, SOLM_UNLOCK)); // <-- EXIT

    return $sql_result;
} // __dzkone__deleteSet




?>
